clear all;
dpi=1250;
sigma = 1e5                                                                                                                                                                                                       ;
threshold = 2;
sample1 = imread('D:\Desktop\test2.png');
sample2 = imread('D:\Desktop\test2.png');
[h,w,c] = size(sample2)
rate = [1/w,1/h]';
rate_reverse = [w,h]';


data= load('D:\Desktop\clusters\type1.txt');
data =data';
offset=[125,130];
data_vec=  load('D:\Desktop\clusters\vec.txt')*dpi+offset;
data_vec =data_vec';
% cx = data(:,1);
% cy=data(:,2);
% cxv = data_vec(:,1);
% cyv=data_vec(:,2);
% figure(1);
% imshow('d:\Desktop\test2.png');
% hold on 
% plot(cxv,cyv,'--','LineWidth',2,'Color','r','Marker','.','MarkerSize',30,'MarkerEdgeColor','green')



set1 = data_vec;
set2 = data;
% set2 = polygon_set2;
[score,z,match_indexes1,match_indexes2,index,G,V,E]=EigenVectorMatch(set1,set2,sigma,threshold);
match_indexes1 = cell2mat(match_indexes1);
match_indexes2= cell2mat(match_indexes2);
[n1,n2]=size(match_indexes1);

    cx1 = set1(1,match_indexes1);
    cy1 = set1(2,match_indexes1);
    
    cx2 = set2(1,match_indexes2);
    cy2 = set2(2,match_indexes2);
    
    figure(1)
    clf

    imshow([sample1,sample2])
    hold on

%     plot(cx1,cy1,'go','LineWidth',2,'MarkerEdgeColor','red','MarkerFaceColor','red','MarkerSize',3)
    for i=1:n2
            plot([cx1(i),cx2(i)+w],[cy1(i),cy2(i)],'--','LineWidth',2,'Color','red','Marker','.','MarkerSize',30,'MarkerEdgeColor','green')
            hold on
    end
    
    
 


% set2 = polygon_set2;
%计算质心
[c,len2]=size(set2);
[c,len]=size(set1);
centroid_set1=[sum(set1(1,:))/len,sum(set1(2,:))/len]';
centroid_set2=[sum(set2(1,:))/len,sum(set2(2,:))/len]';

disp('质心1');disp(centroid_set1);
disp('质心2');disp(centroid_set2);
%点集减去质心
set1= set1-centroid_set1;
set2 = set2-centroid_set2;
disp('去质心点集1');disp(set1);
disp('去质心点集2');disp(set2);
%求 点集1中点*点集2重点转置
w=zeros(2,2);
for i=1:len
    for j=len2
        w=w+set1(:,i)*set2(:,j)';
    end
end

disp('w');disp(w);
disp('进行特征值分解');
[U,S,V]=svd(w);
R=U*V';
T=centroid_set1-R*centroid_set2;
disp('R');disp(R);
disp('T');disp(T);    
